//
//  FancyAddress.swift
//  Fancyswap
//
//  Created by Francois Gratton on 2015-04-12.
//  Copyright (c) 2015 Jonathan Muschalle. All rights reserved.
//

import Foundation

class FancyAddress {
    
    var zip: String = ""
    var city: String = ""
    var state: String = ""
    var country: String = ""
    
    init(){
    }
    init (swiftyDic:JSON) {
        let array = swiftyDic["results"]
        if array.count == 0 {
            return
        }
        
        let addr = array[0]["address_components"].arrayValue
        
        for addrComp in addr
        {
            let type = addrComp["types"].arrayValue
            var typename = ""
            
            for i in 0...(type.count-1){
                typename = type[i].stringValue.lowercaseString
                if typename == "political" {
                    continue
                }
                
                switch typename
                {
                case "postal_code":
                    self.zip = addrComp["long_name"].stringValue
                case "locality":
                    self.city = addrComp["long_name"].stringValue
                case "administrative_area_level_1":
                    self.state = addrComp["long_name"].stringValue
                case "country":
                    self.country = addrComp["long_name"].stringValue
                default:
                    continue
                }
            }
            
        }

    }

    func toString() -> String {
        return "zip: \(zip), city: \(city), state: \(state), country: \(country)"
    }
}